package com.topnav.mc.transmit;

import com.js.lib.cache.SysCache;
import com.js.lib.utils.JsonUtil;
import com.topnav.mc.admin.entity.Device;
import com.topnav.mc.admin.service.IDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Order(1)
@Slf4j
//@Component
public class SystemRunner implements ApplicationRunner {
    @Autowired
    private SysCache cache;
    @Autowired
    private IDeviceService deviceService;
/*    @Autowired
    private IGbDeviceService gbDeviceService;
    @Autowired
    private IGbServerService gbServerService;
    @Autowired
    private IGbChannelService gbChannelService;

    @Autowired
    private JobManagement jobManagement;
    @Autowired
    private IQuartzJobService quartzJobService;*/


    @Override
    public void run(ApplicationArguments args) throws Exception {
/*

        List<Device> deviceList = deviceService.list();
        for(Device device : deviceList){
            cache.hset(Constants.CAHCE_DEVICE_LIST,device.getDeviceCode(),device);
        }
        log.info("设备列表存入缓存.......");

        try{
            // 所有设备和通道推送给 国标平台
            List<DeviceMsg> deviceMsgs = new ArrayList<>();

            List<GbServer> gbServers = gbServerService.list();
            for(GbServer gbServer : gbServers){
                deviceMsgs.clear();

                LambdaQueryWrapper<GbDevice> wrapper = new LambdaQueryWrapper<GbDevice>().eq(GbDevice::getGbServerId,gbServer.getId());
                List<GbDevice> gbDevices = gbDeviceService.list(wrapper);
                for(GbDevice gbDevice : gbDevices){
                    */
/** 组装成传输的数据*//*

                    DeviceMsg deviceMsg = new DeviceMsg().setMethod(1)
                            .setDeviceInfo(new DeviceInfo()
                                    .setId(gbDevice.getId())
                                    .setDeviceName(gbDevice.getDeviceName())
                                    .setOriginDeviceId(gbDevice.getGbDeviceId())
                                    .setNewDeviceId(gbDevice.getGbDeviceId()));
                    deviceMsgs.add(deviceMsg);
                }
                // 设备列表发送到 国标平台

                if (deviceMsgs.size() > 0){
                    TransmitData transmitData = new TransmitData();
                    transmitData.setType("DEVICELIST");
                    transmitData.setData(deviceMsgs);
                    log.debug(JsonUtil.toJson(transmitData));
                    HttpUtil.doPost(
                            "http://" + gbServer.getHost() + ":" + gbServer.getInterfacePort() + "/api/data/from",
                            JsonUtil.toJson(transmitData),
                            null,
                            30000
                    );
                }


                List<ChannelMsg> channelMsgs = new ArrayList<>();
                for(GbDevice gbDevice : gbDevices){
                    channelMsgs.clear();
                    LambdaQueryWrapper<GbChannel> wrapper1 = new LambdaQueryWrapper<GbChannel>().eq(GbChannel::getDeviceId,gbDevice.getId());
                    List<GbChannel> channels = gbChannelService.list(wrapper1);
                    for(GbChannel gbChannel : channels){
                        ChannelMsg channelMsg = new ChannelMsg().setMethod(1)
                                .setChannelInfo(new ChannelInfo()
                                        .setChannelId(gbChannel.getChannelId())
                                        .setChannelName(gbChannel.getChannelName())
                                        .setOriginChannelId(gbChannel.getChannelId())
                                        .setGbDeviceId(gbDevice.getGbDeviceId())
                                        .setLongitude(gbChannel.getLongitude())
                                        .setLatitude(gbChannel.getLatitude())
                                        .setPtzType(gbChannel.getPtzType()));

                        channelMsgs.add(channelMsg);
                    }
                    if (channelMsgs.size() > 0){
                        */
/** 通道列表发送到 国标平台 *//*

                        TransmitData transmitData1 = new TransmitData();
                        transmitData1.setType("CHANNELLIST");
                        transmitData1.setData(channelMsgs);
                        log.debug(JsonUtil.toJson(transmitData1));
                        HttpUtil.doPost(
                                "http://" + gbServer.getHost() + ":" + gbServer.getInterfacePort() + "/api/data/from",
                                JsonUtil.toJson(transmitData1),
                                null,
                                30000
                        );
                    }

                }
            }
        }catch (Exception e){
            log.error(">>>>>> {}",e.toString());
        }


        //Quartz 默认任务
        setDefaultJob();

        // 查询所有的 JOB
        List<QuartzJob> quartzJobList = quartzJobService.list();
        for(QuartzJob job : quartzJobList){
            if (job.getStatus()!=1){continue;}
            log.info(">>>>>> 启用{}定时任务功能 ......",job.getJobDesc());
            jobManagement.addJob(job.toTask());
        }
        jobManagement.start();
*/


    }

    /**
     * 默认任务，用户不能修改的任务
     */
    private void setDefaultJob(){

        /**
         * 模拟传感器数据上传
         */
        /*QuartzTask displacementTask = new QuartzTask()
                .setJobName("模拟-北斗位移-数据上传")
                .setJobGroupName("模拟传感器数据上传")
                .setTriggerName("模拟传感器数据上传-北斗位移")
                .setTriggerGroupName("模拟传感器数据上传")
                .setCron("0 0/10 * * * ? *") // 每20分钟触发一次
                //.setCron("0 0/2 * * * ? *") // 每1小时触发一次
                .setJobClass("com.js.detection.quartz.job.SimulationDisplacementJob");
        jobManagement.addJob(displacementTask);*/

        /**
         * 模拟水位传感器数据上传
         */
        /*QuartzTask waterpositionTask = new QuartzTask()
                .setJobName("模拟-水位传感器-数据上传")
                .setJobGroupName("模拟水位传感器数据上传")
                .setTriggerName("模拟水位传感器数据上传-水位监测")
                .setTriggerGroupName("模拟水位传感器数据上传")
                .setCron("0 0/10 * * * ? *") // 每30分钟触发一次
                //.setCron("0 0/5 * * * ? *") // 测试每5分钟触发一次
                .setJobClass("com.js.detection.quartz.job.SimulationWaterpositionJon");
        jobManagement.addJob(waterpositionTask);*/

        /**
         * 模拟温度传感器数据上传
         */
        /*QuartzTask temperratureTask = new QuartzTask()
                .setJobName("模拟-温度传感器-数据上传")
                .setJobGroupName("模拟温度传感器数据上传")
                .setTriggerName("模拟温度传感器数据上传-温度监测")
                .setTriggerGroupName("模拟温度传感器数据上传")
                .setCron("0 0/10 * * * ? *") // 每30分钟触发一次
                //.setCron("0 0/2 * * * ? *") // 测试每5分钟触发一次
                .setJobClass("com.js.detection.quartz.job.SumlationTemperratureJob");
        jobManagement.addJob(temperratureTask);*/

    }
}
